import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/util/HttpUtil.dart';

class UserService{

  // 用户登录
  Future<dynamic> login(String username , String password , String school) async{
    Dio dio = new Dio() ;
    dynamic data ;
    try {
      Response d = await dio.get(Config().getHost()+"/api/v1/login?username="+username+"&password="+password+"&school="+school) ;
      data = d.data ;
      if(data["code"] == 200){
        Config().setToken(d.headers["token"][0]) ;
      }
    } catch (e) {
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  // 用户完善信息
  Future<bool> userinfo(File file , String nickname , String phone , int gender) async{
    var data = await HttpUtil().upload(Config().getHost()+"/api/admin/v1/upload", file) ;
    // 上传成功后完善用户信息
    if(data["code"] == 200){
      var data1 = await HttpUtil().get(Config().getHost()+"/api/admin/v1/fullInfo?icon="+data["data"]+"&nickname="+nickname+"&phone="+phone+"&gender="+gender.toString()) ;
      
      if(data1["code"] == 200){
        dynamic user = jsonDecode(Config().getUserInfo()) ;
        user["nickname"] = nickname ;
        user["phone"] = phone ;
        user["gender"] = gender ;
        user["icon"] = "/image/"+data["data"].toString().substring(data["data"].toString().lastIndexOf("/")) ;
        Config().setUserInfo(user) ;

        return true ;
      }
    }
    return false ;
  }

  // 用户完善信息
  Future<bool> updateUserInfo(File file , String nickname , String phone , String room , String wx) async{
    var data = null ;
    if(file != null){
      data = await HttpUtil().upload(Config().getHost()+"/api/admin/v1/upload", file) ;
    }
    // 上传成功后完善用户信息
    if(file == null || data["code"] == 200){
      String params = "" ;
      if(file != null){
        params = params == "" ? params + "?" : params + "&" ;
        params += "icon="+data["data"].toString().trim() ;
      }
      if(nickname != null){
        params = params == "" ? params + "?" : params + "&" ;
        params += "nickname="+nickname.trim() ;
      }
      if(phone != null){
        params = params == "" ? params + "?" : params + "&" ;
        params += "phone="+phone.trim() ;
      }
      if(room != null){
        params = params == "" ? params + "?" : params + "&" ;
        params += "room="+room.trim() ;
      }
      if(wx != null){
        params = params == "" ? params + "?" : params + "&" ;
        params += "wx="+wx.trim() ;
      }
      var data1 = await HttpUtil().get(Config().getHost()+"/api/admin/v1/updateUserInfo"+params) ;
      
      if(data1["code"] == 200){
        dynamic user = jsonDecode(Config().getUserInfo()) ;
        user["nickname"] = nickname ;
        user["phone"] = phone ;
        user["room"] = room ;
        user["wx"] = wx ;
        if(file != null){
          user["icon"] = "/image/"+data["data"].toString().substring(data["data"].toString().lastIndexOf("/")) ;
        }
        Config().setUserInfo(user) ;

        return true ;
      }
    }
    return false ;
  }

  Future<dynamic> opinion(String content) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/opinion?content="+content) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> addTasks(String content) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/addTask?content="+content) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> updateTasks(String content , String id) async{ 
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/updateTask?id="+id.toString() + "&content="+content) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> delTasks(String id) async{ 
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/delTask?id="+id.toString()) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> changeStatus(String id , String status) async{ 
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/changeStatus?id="+id.toString()+"&status="+status) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }
}